package com.syy.homework1;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class HomeWorkReduce extends Reducer<HomeWork, NullWritable, HomeWork, NullWritable> {
    @Override
    protected void reduce(HomeWork key, Iterable<NullWritable> values, Reducer<HomeWork, NullWritable, HomeWork, NullWritable>.Context context) throws IOException, InterruptedException {
        Iterator<NullWritable> iterator = values.iterator();
        System.out.println(iterator);
        StringBuilder numbers = new StringBuilder();
        while (iterator.hasNext()) {
            iterator.next();
            if (!numbers.toString().endsWith(",") && !numbers.toString().equals("")) {
                numbers.append(",").append(key.getNumber().toString());
            }else {
                numbers.append(key.getNumber().toString());
            }

        }
        key.setNumbers(numbers.toString());

        context.write(key, NullWritable.get());
    }
}
